package com.creditease.mobile.news.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.creditease.mobile.R;
import com.creditease.mobile.news.model.NewsModel;
import com.creditease.mobile.news.util.BitmapWorkerTask;
import com.creditease.mobile.rss.RssConst;
import com.creditease.mobile.util.AsyncImageLoader3;
import com.creditease.mobile.util.FileUtil;
import com.creditease.mobile.util.LruCacheUtil;

import java.io.File;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class NewsAdapter extends BaseAdapter {
    private static final String TAG = NewsAdapter.class.getName();
    private AsyncImageLoader3 asyncImageLoader3 = new AsyncImageLoader3();
    private Context context;

    private List<NewsModel> list;

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public NewsAdapter(Context context, List<NewsModel> list) {
        super();
        this.context = context;
        this.list = list;
    }

    ConcurrentHashMap<Integer, View> viewHold = new ConcurrentHashMap<Integer, View>();

    public void updateView() {
        viewHold.clear();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = viewHold.get(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.news_home_item, null);
            NewsModel item = list.get(position);
            final ImageView pic = (ImageView) convertView
                    .findViewById(R.id.news_pic);
            TextView title = (TextView) convertView.findViewById(R.id.news_title);
            TextView desc = (TextView) convertView.findViewById(R.id.news_desc);

            title.setText(item.title);
            desc.setText(item.digest);
            String path = item.imgsrc;
            if (item.imgsrc == null || "".equals(item.imgsrc.trim())) {
                String key = "nopic";
                Bitmap bitmap = getResourceBitmap(key, R.drawable.nopic);
                pic.setImageBitmap(bitmap);
                return convertView;
            }

            String newFileName = FileUtil.getFileName(path);
            // 后缀
            final String filePath = RssConst.NEWS_IMG_CACHE + newFileName;
            File file = new File(filePath);
            String key = "loading";
            if (!file.exists()) {
                Bitmap bitmap = getResourceBitmap(key, R.drawable.loading);
                pic.setImageBitmap(bitmap);
                Log.d(TAG, "从网络加载图片：" + item.imgsrc);
                BitmapWorkerTask task = new BitmapWorkerTask();
                task.execute(item.imgsrc, filePath, pic);
            } else {
                Bitmap bmp = LruCacheUtil.getBitmapFromMemCache(item.imgsrc);
                if (bmp != null) {
                    Log.d(TAG, "从缓存加载图片：" + item.imgsrc);
                    pic.setImageBitmap(bmp);
                } else {
                    Bitmap bitmap = getResourceBitmap(key, R.drawable.loading);
                    pic.setImageBitmap(bitmap);
                    Log.d(TAG, "从SD卡加载图片：" + item.imgsrc);
                    BitmapWorkerTask task = new BitmapWorkerTask();
                    task.execute(item.imgsrc, filePath, pic);
                    /*bmp = BitmapFactory.decodeFile(filePath);
                    Bitmap resizeBitmap = ThumbnailUtils.extractThumbnail(bmp, 80, 60, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
                    pic.setImageBitmap(resizeBitmap);
                    LruCacheUtil.addBitmapToCache(path, resizeBitmap);*/
                }

                /*BitmapWorkerTask task = new BitmapWorkerTask();
             task.execute(filePath, 80, 60, pic);*/
            }
            viewHold.put(position, convertView);
        }
        return convertView;
    }

    private Bitmap getResourceBitmap(String path, int sourceId) {
        String key = path;
        Bitmap bitmap = LruCacheUtil.getBitmapFromMemCache(key);
        if (bitmap == null) {
            Log.d(TAG, "从SD卡加载图片：" + key);
            Bitmap bitmap2 = BitmapFactory.decodeResource(context.getResources(), sourceId);
            bitmap = ThumbnailUtils.extractThumbnail(bitmap2, 80, 60, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
            LruCacheUtil.addBitmapToCache(key, bitmap);
        } else {
            Log.d(TAG, "从缓存加载图片：" + key);
        }
        return bitmap;
    }
}
